package com.yunfeisoft.base.dao.impl;

import com.yunfeisoft.base.dao.inter.WorkflowHistoryDao;
import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.model.WorkflowDefine;
import com.yunfeisoft.base.model.WorkflowHistory;
import com.yunfeisoft.base.model.WorkflowInstance;
import com.yunfeisoft.common.base.BaseDaoImpl;
import com.yunfeisoft.common.sql.builder.SelectBuilder;
import com.yunfeisoft.common.sql.builder.WhereBuilder;
import com.yunfeisoft.common.utils.Page;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * ClassName: WorkflowHistoryDaoImpl
 * Description: 流程实例执行历史Dao实现
 * Author: Jackie liu
 * Date: 2020-01-03
 */
@Repository
public class WorkflowHistoryDaoImpl extends BaseDaoImpl<WorkflowHistory, String> implements WorkflowHistoryDao {

    @Override
    public Page<WorkflowHistory> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithDesc("createTime");
        if (params != null) {
            initPageParam(wb, params);
            wb.andEquals("h.executor", params.get("executor"));
            wb.andEquals("h.status", params.get("status"));
            wb.andFullLike("i.title", params.get("title"));
            wb.andFullLike("u.name", params.get("instanceCreateName"));
        }
        SelectBuilder builder = getSelectBuilder("h");
        builder.column("i.title as instanceTitle")
                .column("i.formId as instanceFormId")
                .column("d.formPath as defineFormPath")
                .column("u.name as instanceCreateName")
                .column("us.name as executorName")
                .column("i.workflowDefineId as workflowDefineId")
                .join(WorkflowInstance.class).alias("i").on("h.workflowInstanceId = i.id").build()
                .join(WorkflowDefine.class).alias("d").on("i.workflowDefineId = d.id").build()
                .join(User.class).alias("u").on("i.createId = u.id").build()
                .join(User.class).alias("us").on("h.createId = us.id").build();

        return queryPage(builder.getSql(), wb);
    }

    @Override
    public List<WorkflowHistory> queryList(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithDesc("createTime");
        if (params != null) {
            initPageParam(wb, params);
            wb.andEquals("i.formId", params.get("formId"));
        }
        SelectBuilder builder = getSelectBuilder("h");
        builder.column("i.title as instanceTitle")
                .column("i.formId as instanceFormId")
                //.column("d.formPath as defineFormPath")
                //.column("u.name as instanceCreateName")
                .column("us.name as executorName")
                .column("i.workflowDefineId as workflowDefineId")
                .join(WorkflowInstance.class).alias("i").on("h.workflowInstanceId = i.id").build()
                //.join(WorkflowDefine.class).alias("d").on("i.workflowDefineId = d.id").build()
                //.join(User.class).alias("u").on("i.createId = u.id").build()
                .join(User.class).alias("us").on("h.createId = us.id").build();

        return query(builder.getSql(), wb);
    }
}